
dv <- 'courtlegit_composite'

# #filter for variable of interest 
#personal_dobbs, court_dobbs, personal_roe, court_roe
filtered_panel <- sum_panel_ideo_full %>%  filter(variable == dv)
filtered_cross <- sum_cross_ideo_full %>%  filter(variable == dv)
plot_data <- rbind(filtered_panel, filtered_cross)

plot_data <- plot_data %>% slice(-c(4))
plot_data$sample <- c("panel", "panel", "panel", "cross", "cross")
sum(plot_data$N[plot_data$sample == "panel"])
sum(plot_data$N[plot_data$sample != "panel"])

plot_data$alignment <- c(1,2,3,4,5)

plot_data$wave <- recode(plot_data$wave, '1' = "Pre-leak\n(April 2022)", '2' = "Post-leak\n(May 2022)", '3'="Post-ruling\n(June 2022)" )

plot_data$wave <- fct_relevel(plot_data$wave,  "Pre-leak\n(April 2022)", "Post-leak\n(May 2022)","Post-ruling\n(June 2022)")

plot_data$wave <- as.factor(plot_data$wave)
plot_data$tempvar <- "A. All Data"


#plot

(a <- plot_data %>%  
    ggplot(aes(y=mean, x=wave, color=sample, group=alignment)) + 
    geom_errorbar(width = 0, aes(ymin=(mean-se*1.96), ymax=(mean+se*1.96)), size=1,color = c("#D55E00","#D55E00","#D55E00","#0072B2","#0072B2"), position = position_dodge(width=.31)) +

    geom_point(aes(color=sample), size = 2, shape=c(21, 21, 21, 22, 22), fill =c("#D55E00","#D55E00","#D55E00","#0072B2","#0072B2"),  color = c("#D55E00","#D55E00","#D55E00","#0072B2","#0072B2"), position = position_dodge(width=.31)) + 
    theme_bw() + 
    scale_y_continuous(limits = c(.35,.6)) +
    theme(axis.text.y = element_text(size = 12),
          axis.text.x = element_text(size = 12),
          axis.title.y = element_text(size = 12, vjust = -.1),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank()) +
    ylab("Mean reported\nlegitmacy of the Court") +
    xlab("") +
    
    geom_vline(xintercept=1.5, linetype="dashed",color="gray") +
    geom_vline(xintercept=2.5, linetype="dashed",color="gray") +
    geom_segment(yend = plot_data$mean[1], xend = 1, y = plot_data$mean[2], x = 2-.1, color = "#D55E00", linetype="dotted")+
    geom_segment(yend = plot_data$mean[2], xend = 2-.1, y = plot_data$mean[3], x = 3-.1, color = "#D55E00", linetype="dotted")+
    facet_grid(. ~ tempvar) +
    theme(strip.background = element_rect(fill="#000000"),
          strip.text = element_text(size=15, colour="white"))
)



##### Dems

filtered_panel <- sum_panel_ideo %>%  filter(variable == dv) %>% filter(pid == "Democrat")
filtered_cross <- sum_cross_ideo %>%  filter(variable == dv) %>% filter(pid == "Democrat")
plot_data <- rbind(filtered_panel, filtered_cross)
plot_data <- plot_data %>% slice(-c(4))
plot_data$sample <- c("panel", "panel", "panel", "cross", "cross")
sum(plot_data$N[plot_data$sample == "panel"])
sum(plot_data$N[plot_data$sample != "panel"])

plot_data$alignment <- c(1,2,3,4,5)

plot_data$wave <- recode(plot_data$wave, '1' = "Pre-leak\n(April 2022)", '2' = "Post-leak\n(May 2022)", '3'="Post-ruling\n(June 2022)" )

plot_data$wave <- fct_relevel(plot_data$wave,  "Pre-leak\n(April 2022)", "Post-leak\n(May 2022)","Post-ruling\n(June 2022)")

plot_data$wave <- as.factor(plot_data$wave)
plot_data$tempvar <- "B. Democrats"
#plot
(b<-plot_data %>%  
    ggplot(aes(y=mean, x=wave, color=sample, group=alignment)) + 
    geom_errorbar(width= 0, aes(ymin=(mean-se*1.96), ymax=(mean+se*1.96)), color = c("#D55E00","#D55E00","#D55E00","#0072B2","#0072B2"), position = position_dodge(width=.31), size=1) +
  geom_point(aes(color=sample), size = 2, shape=c(21, 21, 21, 22, 22), fill =c("#D55E00","#D55E00","#D55E00","#0072B2","#0072B2"),  color = c("#D55E00","#D55E00","#D55E00","#0072B2","#0072B2"), position = position_dodge(width=.31)) + 
    theme_bw() + 

    scale_y_continuous(limits = c(.35,.6)) +
    theme(axis.text.y = element_text(size = 12),
          axis.text.x = element_text(size = 12),
          axis.title.x = element_text(size = 12, vjust = -.1),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.background = element_rect(fill = alpha("#99ccff",.1),
                                          colour = alpha("#99ccff",.1),
                                          size = 0.5, linetype = "solid") ) +
    xlab("") +
    ylab("Mean reported\nlegitmacy of the Court") +
    
    geom_vline(xintercept=1.5, linetype="dashed",color="gray") +
    geom_vline(xintercept=2.5, linetype="dashed",color="gray") +
    geom_segment(yend = plot_data$mean[1], xend = 1, y = plot_data$mean[2], x = 2-.1, color = "#D55E00", linetype="dotted")+
    geom_segment(yend = plot_data$mean[2], xend = 2-.1, y = plot_data$mean[3], x = 3-.1, color = "#D55E00", linetype="dotted")+
    facet_grid(. ~ tempvar) +
    theme(strip.background = element_rect(fill="#000000"),
          strip.text = element_text(size=15, colour="white"))
)


ggsave(plot=b, "plots/temp.pdf", width=8)
##### Reps

filtered_panel <- sum_panel_ideo %>%  filter(variable == dv) %>% filter(pid == "Republican")
filtered_cross <- sum_cross_ideo %>%  filter(variable == dv) %>% filter(pid == "Republican")
plot_data <- rbind(filtered_panel, filtered_cross)
plot_data <- plot_data %>% slice(-c(4))
plot_data$sample <- c("panel", "panel", "panel", "cross", "cross")
sum(plot_data$N[plot_data$sample == "panel"])
sum(plot_data$N[plot_data$sample != "panel"])

plot_data$alignment <- c(1,2,3,4,5)

plot_data$wave <- recode(plot_data$wave, '1' = "Pre-leak\n(April 2022)", '2' = "Post-leak\n(May 2022)", '3'="Post-ruling\n(June 2022)" )

plot_data$wave <- fct_relevel(plot_data$wave,  "Pre-leak\n(April 2022)", "Post-leak\n(May 2022)","Post-ruling\n(June 2022)")

plot_data$wave <- as.factor(plot_data$wave)
plot_data$tempvar <- "C. Republicans"
#plot
(c <- plot_data %>%  
    ggplot(aes(y=mean, x=wave, color=sample, group=alignment)) + 
    geom_errorbar(width = 0, aes(ymin=(mean-se*1.96), ymax=(mean+se*1.96)), color = c("#D55E00","#D55E00","#D55E00","#0072B2","#0072B2"), position = position_dodge(width=.31), size=1) +
   
    geom_point(aes(color=sample), size = 2, shape=c(21, 21, 21, 22, 22), fill =c("#D55E00","#D55E00","#D55E00","#0072B2","#0072B2"),  color = c("#D55E00","#D55E00","#D55E00","#0072B2","#0072B2"), position = position_dodge(width=.31)) + 
    theme_bw() +
    scale_y_continuous(limits = c(.5,.75)) +
    theme(axis.text.y = element_text(size = 12),
          axis.text.x = element_text(size = 12),
          axis.title.x = element_text(size = 12, vjust = -.1),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.background = element_rect(fill = alpha("#ff9999",.1),
                                          colour = alpha("#fbeaea",.1),
                                          size = 0.5, linetype = "solid") )+
    
    ylab("Mean reported\nlegitmacy of the Court") +
    xlab("") +
    
    geom_vline(xintercept=1.5, linetype="dashed",color="gray") +
    geom_vline(xintercept=2.5, linetype="dashed",color="gray") +
    geom_segment(yend = plot_data$mean[1], xend = 1, y = plot_data$mean[2], x = 2-.1, color = "#D55E00", linetype="dotted")+
    geom_segment(yend = plot_data$mean[2], xend = 2-.1, y = plot_data$mean[3], x = 3-.1, color = "#D55E00", linetype="dotted")+
    facet_grid(. ~ tempvar) +
    theme(strip.background = element_rect(fill="#000000"),
          strip.text = element_text(size=15, colour="white"))
)

ggsave(plot=c, "plots/temp.pdf", width=8)

#legend

legend <- ggplot(plot_data, aes(x = mean, y = wave, color=sample) ) +
  geom_errorbarh(height = 0, aes(xmin = mean, xmax = mean), position = position_dodge(width=.51),show.legend=FALSE) +
  geom_point(size = 9, shape=21, fill ="white",position = position_dodge(width=.51)) + theme_bw() + 
  scale_color_discrete(labels=c('Cross section', 'Panel')) +
  guides(color=guide_legend("", override.aes = list(shape=c(15,19),size=4, color=c("#0072B2","#D55E00")))) + 
  theme(legend.position="bottom") 

legend <- cowplot::get_legend(legend)

title <- ggdraw() + draw_label("Perceptions of Court Legitimacy", fontface='bold')             

plot <- plot_grid(a, b, c, label_size = 12, ncol=1,rel_heights=c(1,1,1))

(plot1 <- plot_grid(title, plot, legend,  ncol = 1,rel_heights=c(.05,1,.05)))

ggsave(plot = plot1, "plots/plot2.pdf", width=4, height=7)

